package com.pandy.disruptor.quickstart;

import com.lmax.disruptor.BlockingWaitStrategy;
import com.lmax.disruptor.RingBuffer;
import com.lmax.disruptor.dsl.Disruptor;
import com.lmax.disruptor.dsl.ProducerType;

import java.nio.ByteBuffer;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.atomic.AtomicInteger;
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @description: 启动类
 * @author: panhong
 * @date: 2025/4/10
 */
public class Main {

    public static void main(String[] args) {
        OrderEventFactory eventFactory = new OrderEventFactory();
        int ringBufferSize = 1024 * 1024;
        ExecutorService executorService = Executors.newFixedThreadPool(Runtime.getRuntime().availableProcessors());

        // 创建Disruptor对象
        Disruptor<OrderEvent> disruptor = new Disruptor<>(eventFactory, ringBufferSize,
                executorService, ProducerType.SINGLE,
                new BlockingWaitStrategy());

        // 添加消费者监听
        disruptor.handleEventsWith(new OrderEventHandler());

        // 启动 disruptor
        disruptor.start();

        // 获取实际存储数据的容器：RingBuffer
        RingBuffer<OrderEvent> ringBuffer = disruptor.getRingBuffer();

        // 创建生产者组件
        OrderEventProducer producer = new OrderEventProducer(ringBuffer);

        ByteBuffer bb = ByteBuffer.allocate(8);

        for(int i=0; i<100; i++) {
            bb.putLong(0, i);

            producer.sendData(bb);
        }

        // 关闭操作
        disruptor.shutdown();
        executorService.shutdown();
    }

    public static void test() {

    }
}
